<template>
	<!-- 功能列表 -->
	<view>
		<view class="item-box" style="margin-top: 30rpx;">
			<image style="width: 40rpx;" src="../../static/fxys.png" mode="widthFix"></image>
			<view class="u-flex-1 m-l-20">分享钥匙</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="item-box">
			<image style="width: 40rpx;" src="../../static/czrz.png" mode="widthFix"></image>
			<view class="u-flex-1 m-l-20">操作日志</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="item-box" @click="goHandMovement">
			<image style="width: 40rpx;" src="../../static/sdms.png" mode="widthFix"></image>
			<view class="u-flex-1 m-l-20">手动模式</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="item-box" @click="goNearUnlock">
			<image style="width: 40rpx;" src="../../static/jjks.png" mode="widthFix"></image>
			<view class="u-flex-1 m-l-20">接近开锁</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="item-box" @click="gofirmwareUpdate">
			<image style="width: 40rpx;" src="../../static/gjsj.png" mode="widthFix"></image>
			<view class="u-flex-1 m-l-20">固件升级</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="item-box" @click="goDeviceInfo">
			<image style="width: 40rpx;" src="../../static/sbxx.png" mode="widthFix"></image>
			<view class="u-flex-1 m-l-20">设备信息</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="bottom-btn" @click="onClickDelete">删除钥匙</view>
	</view>
</template>

<script>
	import {
		guid
	} from "../../utils/util";
	import {
		getUuid,
		writeBLECharacteristicValue
	} from "../../utils/common";
	import {
		getDevice,
		setDevice,
		removeLastConnectedDevice
	} from "../../utils/storage";
	const app = getApp();
	export default {
		data() {
			return {

			};
		},
		onLoad(options) {

		},
		onShow() {

		},
		methods: {

			goNearUnlock() {
				uni.navigateTo({
					url: "/pages/near-unlocking/near-unlocking"
				})
			},
			goHandMovement() {
				uni.navigateTo({
					url: "/pages/hand-movement/hand-movement"
				})
			},
			gofirmwareUpdate() {
				uni.navigateTo({
					url: "/pages/firmware-update/firmware-update"
				})
			},
			goDeviceInfo() {
				uni.navigateTo({
					url: "/pages/device-info/device-info"
				})
			},
			onClickDelete() {
				uni.showModal({
					title: "提示",
					content: "确定要删除选中的钥匙？",
					success: (res) => {
						if (res.confirm) {
							this.handleDeleteKeys();
						}
					},
				});
			},
			handleDeleteKeys() {
				const _this = this;
				const serviceId = getUuid("5508");
				writeBLECharacteristicValue(serviceId, getUuid("0062"), "", "", (res) => {
					console.log(res.code, app.globalData.deviceId, getDevice());
					if (res.code === 200) {
						const deviceList = getDevice();
						const currentDeviceList = deviceList.filter((item) => item.deviceId !== app.globalData
							.deviceId);
						setDevice(currentDeviceList);
						removeLastConnectedDevice();
						uni.showModal({
							title: "提示",
							content: "删除成功",
							showCancel: false,
							confirmText: "好",
							success() {
								_this.closeBLEConnection();
								uni.redirectTo({
									url: "/pages/index/index"
								});
							},
						});
					} else {
						uni.showToast({
							title: "删除失败",
							icon: "error",
						});
					}
				});
			},
			closeBLEConnection() {
				uni.closeBLEConnection({
					deviceId: app.globalData.deviceId,
					success() {
						console.log("closeBLEConnection success");
					},
					fail(error) {
						console.log("closeBLEConnection fail", error);
					},
				});
				app.globalData.deviceId = "";
			},
		}
	}
</script>

<style lang="scss" scoped>
	.item-box {
		border-bottom: 1px solid #EAE7E7;
		display: flex;
		padding: 30rpx 40rpx;
		background-color: white;
	}

	.bottom-btn {
		background-color: #E8E8E8;
		text-align: center;
		position: fixed;
		bottom: 40rpx;
		width: 450rpx;
		left: 150rpx;
		border-radius: 10rpx;
		color: red;
		padding: 20rpx 0;
		font-size: 14px;
	}
</style>